<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Traits\InjectServices;
use Closure;

class CheckUserToken
{
    use InjectServices;

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     * @throws \App\Exceptions\InvalidToken
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('X-USER-TOKEN');
        $user = $this->authService->tokenToUser($token);
        $request->setUserResolver(function () use ($user){
            return $user;
        });
        return $next($request);
    }
}
